using System;
using System.Collections.Generic;
using System.Text;
using Colman.Rpg;
using System.Collections;
using Colman.Rpg.Npc;
using System.Xml.Serialization;
using System.IO;
using Colman.Rpg.Messaging;
using Colman.Rpg.Server.Workflow;

namespace Colman.Rpg.Server
{
    /// <summary>
    /// Handles server objects' workflow
    /// </summary>
    public static class WorkFlow
    {
        /// <summary>
        /// 
        /// </summary>
        private static Dictionary<string, int> pointsOwed = new Dictionary<string, int>();

        /// <summary>
        /// Holds all server known messages types and pointer to workflow's response handler
        /// </summary>
        private static Dictionary<Type, WorkflowHandler> handlers = new Dictionary<Type, WorkflowHandler>();
        
        /// <summary>
        /// initialize known handlers
        /// </summary>
        static WorkFlow()
        {
            #region | Initialize handlers (command pattern) |
            handlers.Add(typeof(CreateWorldRequest),
                new WorkflowHandler(NpcWorkflowHandler.CreateResponses));

            handlers.Add(typeof(FightRequest),
                new WorkflowHandler(NpcWorkflowHandler.CreateResponses));
            handlers[typeof(FightRequest)] += PvpWorkflowHandler.CreateResponses;

            handlers.Add(typeof(FightMonsterRequest),
                new WorkflowHandler(NpcWorkflowHandler.CreateResponses));

            handlers.Add(typeof(ChatMessageRequest),
                new WorkflowHandler(PvpWorkflowHandler.CreateResponses));

            handlers.Add(typeof(GetConnectedUsersRequest),
                new WorkflowHandler(PvpWorkflowHandler.CreateResponses));

            handlers.Add(typeof(FightPlayerRequest),
                new WorkflowHandler(PvpWorkflowHandler.CreateResponses));

            handlers.Add(typeof(PlayerPointsAdditionRequest),
                new WorkflowHandler(PlayerControlWorkflowHandler.CreateResponses));

            handlers.Add(typeof(EnterShopRequest),
                new WorkflowHandler(PlayerControlWorkflowHandler.CreateResponses));

            handlers.Add(typeof(BuyItemRequest),
                new WorkflowHandler(PlayerControlWorkflowHandler.CreateResponses));

            handlers.Add(typeof(SellItemRequest),
                new WorkflowHandler(PlayerControlWorkflowHandler.CreateResponses));

            handlers.Add(typeof(EquipArmorRequest),
                new WorkflowHandler(PlayerControlWorkflowHandler.CreateResponses));

            handlers.Add(typeof(EquipWeaponRequest),
                new WorkflowHandler(PlayerControlWorkflowHandler.CreateResponses));

            handlers.Add(typeof(PlayerInformationRequest),
                new WorkflowHandler(PlayerControlWorkflowHandler.CreateResponses));

            handlers.Add(typeof(ChatRoomLeaveRequest),
                new WorkflowHandler(PvpWorkflowHandler.CreateResponses));

            #endregion
        }

        /// <summary>
        /// Gets request from ServerEndPoints and generates the reponses objects
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static IEnumerable<ResponseMessage> CreateResponses(RequestMessage request)
        {
            // holds data about the current request and will keep the response objects
            WorkflowHandlerArgs e = new WorkflowHandlerArgs(request);

            // checks if the request type is known
            if (handlers.ContainsKey(request.GetType()))
            {
                try
                {
                    // activates the appropitiate handler to handle the current request
                    handlers[request.GetType()](e);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    System.Diagnostics.Debugger.Break();
                    throw;
                }
            }
            else
            {
                Console.WriteLine("Unknown request from client '" + request.UserName + "'.");
            }

            return e.Responses;
        }
    }
}
